# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.native_public_client_base as npubcb
import coin.exchange.bitstamp_v2.kr_rest.constants as exchange_constants


class BitstampNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self, timeout=1):
    npubcb.NativePublicClientBase.__init__(self,
                                           url="https://www.bitstamp.net/api",
                                           timeout=timeout)
    self.valid_time = ['minute', 'hour', 'day', None]

  # Last 24 hours ticker
  def get_ticker(self, currency_pair):
    url = "v2/ticker/%s" % currency_pair
    update = self._get_as_json(url)
    return update

  # Last hour ticker
  def get_hourly_ticker(self, currency_pair):
    url = "v2/ticker_hour/%s" % currency_pair
    update = self._get_as_json(url)
    return update

  def get_order_book(self, currency_pair):
    url = "v2/order_book/%s" % currency_pair
    update = self._get_as_json(url)
    return update

  def get_transactions(self, currency_pair, time=None):
    url = "v2/transactions/%s" % currency_pair
    assert time in self.valid_time
    params = {
        "time": time,
    }
    update = self._get_as_json(url, params)
    return update

  def get_trading_pairs_info(self):
    url = "v2/trading-pairs-info"
    update = self._get_as_json(url)
    return update

  def get_eur_usd_conversion_rate(self):
    url = "eur_usd"
    update = self._get_as_json(url)
    return update

  def queryAll(self):
    for symbol in exchange_constants.symbol_list:
      print(self.get_order_book(symbol))


if __name__ == "__main__":
  client = BitstampNativePublicClient(timeout=10)
  print(client.get_trading_pairs_info())
  print(client.get_transactions('btcusd', 'minute'))
  client.queryAll()
